package webInterfaces;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.asthmate.AttackActivity;
import com.example.asthmate.BluetoothActivity;
import com.example.asthmate.EnvironmentActivity;
import com.example.asthmate.HealthStateActivity;
import com.example.asthmate.HistoryActivity;
import com.example.asthmate.LoginActivity;
import com.example.asthmate.MainScreen;
import com.example.asthmate.RegisterActivity;
import com.example.asthmate.RiskMap;
import com.example.asthmate.SetPreferencesActivity;

import util.GPSUtil;
import util.UserTasks;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.widget.Toast;

public class WebUserInterface {
	Context mContext;   

    /** Instantiate the interface and set the context */
    public WebUserInterface(Context c) {
        mContext = c;
    }

  //@JavascriptInterface

    //get Location from the service and send it further to the webview
    public String getLocation(){
    	GPSUtil gps=new GPSUtil(mContext);
		System.out.println(gps.getLongitude());
        if (gps.canGetLocation()){
        	JSONObject json=new JSONObject();
        	try {
				json.put("lat", gps.getLatitude());
				json.put("lon", gps.getLongitude());
				json.put("address", gps.getAddress());				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return json.toString();

        }
        else{
        	gps.showSettingsAlert();
        	return "No provider availables";
        }
    }
    
    
    /** Show a toast from the web page */
    
    public void showToast(String toast, String type) {
        if(type.equals("register")) Toast.makeText(mContext, "Registration successful!", Toast.LENGTH_SHORT).show();
    }
    public void switchToRegister(){
    	Intent i=new Intent(mContext, RegisterActivity.class);
    	mContext.startActivity(i);
    }
    public void switchToMainScreen(){
    	Intent i=new Intent(mContext, MainScreen.class);
    	mContext.startActivity(i);
    }
    public void switchToAttackScreen(){
    	Intent i=new Intent(mContext, AttackActivity.class);
    	mContext.startActivity(i);
    }
    public void switchToHealthState(){
    	Intent i=new Intent(mContext, HealthStateActivity.class);
    	mContext.startActivity(i);
    }
    public void switchToLogin(){
    	Intent i=new Intent(mContext, LoginActivity.class);
    	mContext.startActivity(i);
    }
    public void switchToHistory(){
    	Intent i=new Intent(mContext, HistoryActivity.class);
    	mContext.startActivity(i);
    }
    public void switchToEnvironment(){
    	Intent i=new Intent(mContext, EnvironmentActivity.class);
    	mContext.startActivity(i);
    }
    public void switchToMap(){
    	Intent i=new Intent(mContext, RiskMap.class);
    	mContext.startActivity(i);
    }
    public void switchToBluetooth(){
    	Intent i=new Intent(mContext, BluetoothActivity.class);
    	mContext.startActivity(i);
    }
    public void switchToPreferences(){
    	Intent i=new Intent(mContext, SetPreferencesActivity.class);
    	mContext.startActivity(i);
    }
    /*public void loginError(){
    	Toast.makeText(mContext, "Your login was not successful! Please check username and password!", Toast.LENGTH_SHORT);
    }*/
    public void vibrate(){
    	 //performHapticFeedback(3);
    	Vibrator v = (Vibrator) mContext.getSystemService(Service.VIBRATOR_SERVICE);
    	v.vibrate(150);
    }
    /** Show a toast from the web page */
    public void showMessage(String message){
        Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();  	
    }
    public void showLongMessage(String message){
        Toast.makeText(mContext, message, Toast.LENGTH_LONG).show();  	
    }
}
